<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class FeedBack extends AdminController {

	function __construct() { 
		parent::__construct();
		$this -> load -> library("ProjectService/feedback_service");
		$this -> load -> library("ProjectService/admin_service");		
		$this->load->model('Admin_model');
		$this->load->model('Student_feedback_model');
		$this->load->model('Student_feedback_record_model');
		$this->load->model('Supervisor_feedback_model');
		$this->load->model('Supervisor_feedback_record_model');	
	}
	//转发（个人）
	public function IndexTeacher() {
		$data['show']=false;
		$data['status']=false;
		if(isset($_GET['type'])){
		  $data['feedback_type']= $_GET['type'];
		}else{
			$data['feedback_type']= '';
		}
		$data['a']=0;
		$data['role']=WebRole::Teacher[XPHEnum::Value];
		$this -> load -> view('admin/feedbackList',$data);
	}
	//管理员
	public function IndexManage() {
		$data['show']=false;
		$data['status']=true;
		if(isset($_GET['type'])){
		  $data['feedback_type']= $_GET['type'];
		}else{
			$data['feedback_type']= '';
		}	
		$data['role']=WebRole::Manage[XPHEnum::Value];
		$this -> load -> view('admin/feedbackList',$data);
	}
	//二级教学单位组长
	public function IndexUnitSupervisorGroupLeader() {
		$data['show']=false;
		$data['status']=true;
		if(isset($_GET['type'])){
		  $data['feedback_type']= $_GET['type'];
		}else{
			$data['feedback_type']= '';
		}	
		$data['role']=WebRole::UnitSupervisorGroupLeader[XPHEnum::Value];
		$this -> load -> view('admin/feedbackList',$data);
	}
	//督导处长
	public function IndexSupervisorDirector() {
		$data['show']=true;
		$data['status']=false;
		if(isset($_GET['type'])){
		  $data['feedback_type']= $_GET['type'];
		}else{
			$data['feedback_type']= '';
		}	
		$data['role']=WebRole::SupervisorDirector[XPHEnum::Value];
		$this -> load -> view('admin/feedbackList',$data);
	}
	//校级领导
	public function IndexSchoolLeader() {
		$data['show']=false;
		$data['status']=false;
		if(isset($_GET['type'])){
		  $data['feedback_type']= $_GET['type'];
		}else{
			$data['feedback_type']= '';
		}
		$data['role']=WebRole::SchoolLeader[XPHEnum::Value];
		$this -> load -> view('admin/feedbackList',$data);
	}

	function getDatas($feedback_type,$role,$params = array(), $isExport = false) {
		$model=$feedback_type.'_feedback_model';
		$table='db_'.(new FeedbackType)->showByIndex($feedback_type,XPHEnum::Remark).'_feedback';
		$connar = array();
		if (!$isExport) {
			$params = $_POST;
			$connar = $this -> $model -> pickPages($connar, $params);
		}
		$role_id=explode(',',getSess()['sub_role_id']);
		array_push($role_id,getSess()['role_id']);
		if(in_array(WebRole::Manage[XPHEnum::Value],$role_id)){
			$connar['orderby'] = " role_belong desc,status asc,id desc";
		}else{
			$connar['orderby'] = "status asc,id desc";
		}
		
		$connar['join'][0]=array('admin ac','ac.id='.$table.'.creator');
		$connar['join'][1]=array('admin ab','ab.id='.$table.'.belong');
		$connar['where']=' del_flag= '.CommonStatus::UnDeleted[XPHEnum::Value];

		

		//根据当前role_belong的可见范围
		$admin=$this->Admin_model->get_single(array('id'=>getSess()['id']));

		// $connar['where'].=$this->sqlEscape(" and (find_in_set(".$table.".role_belong,??) or ".$table.".belong=??) ",array($admin['role_id'].$admin['sub_role_id'],$admin['id']));
		//不同角色页面
		if($role==WebRole::Teacher[XPHEnum::Value]){
			$connar['where'].=$this->sqlEscape(" and ".$table.".belong=?? ",array($admin['id']));
		}
		else if($role==WebRole::Manage[XPHEnum::Value]){
			// $connar['where'].=$this->sqlEscape(" and ".$table.".role_belong=?? ",array(WebRole::Manage[XPHEnum::Value]));
		}
		else if($role==WebRole::UnitSupervisorGroupLeader[XPHEnum::Value]){//督导组长
			$connar['where'].=$this->sqlEscape(" and (".$table.".role_belong=?? or ".$table.".role_belong_new=??) ",array(WebRole::UnitSupervisorGroupLeader[XPHEnum::Value],WebRole::UnitSupervisorGroupLeader[XPHEnum::Value]));
			// $connar['where'] .= $this->sqlLikeEscape(" and ac.dept_name like '%??%' ",array($params['param']));
			$connar['where'].=$this->sqlEscape(" and ac.dept_id=?? ",array($admin['dept_id']));
		}
		else if($role==WebRole::SupervisorDirector[XPHEnum::Value]){ //督导处长
			$connar['where'].=$this->sqlEscape(" and (".$table.".status=?? or ".$table.".status=?? ) ",array(FeedBackStatus::HaveWrite[XPHEnum::Value],FeedBackStatus::End[XPHEnum::Value]));
		}
		else if($role==WebRole::SchoolLeader[XPHEnum::Value]){//校领导
			$connar['where'].=$this->sqlEscape(" and ".$table.".show=?? ",array(CommonWhether::Whether[XPHEnum::Value]));
		}
		else{
			$connar['where'].=" 1=-1 ";
		}


		$connar['items']=$table.".*,ac.username creator_char,ifnull(ab.username,'--') belong_char ";

	
		if (isset($params['param'])) {	
			$connar['where'] .= $this->sqlLikeEscape(" and (".$table.".class_name like '%??%' ",array($params['param']));
			$connar['where'] .= $this->sqlLikeEscape(" or ac.username like '%??%') ",array($params['param']));
		}

		if (isset($params['keyword'])) {	
			$connar['where'] .= $this->sqlLikeEscape(" and (".$table.".content like '%??%' ",array($params['keyword']));
			$connar['where'] .= $this->sqlLikeEscape(" or ".$table.".result like '%??%') ",array($params['keyword']));
		}
		

		//分院
		if (isset($params['dept_name'])&&$params['dept_name']>-1) {
			// 公共基础教学部，数据查找为公共基础教学部以及人文社科部
			if($params['dept_name']=='公共基础教学'){
				$connar['where'] .= $this->sqlLikeEscape(" and (".$table.".dept_name like '%??%' or ".$table.".dept_name like '%??%')",array($params['dept_name'],'人文社科'));
			}else{				
				$connar['where'] .= $this->sqlLikeEscape(" and ".$table.".dept_name like '%??%' ",array($params['dept_name']));
			}
			
		}

		if (isset($params['status'])&&$params['status']>0) {
			$connar['where'] .= $this->sqlEscape(" and ".$table.".status=?? ",array($params['status']));
		}
		if (isset($params['show'])&&$params['show']>-1) {
			$connar['where'] .= $this->sqlEscape(" and ".$table.".show=?? ",array($params['show']));
		}
		//筛选
		if (isset($params['checkbox'])) {
			$connar['where'] .= $this->sqlEscape(" and ".$table.".belong>?? ",array(0));
			// $connar['where'] .= $this->sqlEscape(" and ".$table.".belong > ?? ",0);

		}


		if(isset($params['start_time']) && $params['start_time'] != "" && isset($params['end_time']) && $params['end_time'] != ""){
			$start=strtotime($params['start_time'].'00:00:00');
			$end=strtotime($params['end_time'].'23:59:59');
			$connar['where'] .= $this->sqlEscape(" and ".$table.".created>=?? and ".$table.".created<=?? ",array($start,$end));
		}

		

		if (!$isExport) {
			$data = $this -> $model -> get_page_list($connar);

			// echo $this->db->last_query();exit();

		} else {
			$data["ret"] = $this -> $model -> get_list_full($connar);
		}
		
		 //exit();
		$feedback_type_class=new FeedbackType;
		if($feedback_type==FeedbackType::Supervisor[XPHEnum::Value]){
			$type_class=new SupervisorFeedbackType;
		}
		else if($feedback_type==FeedbackType::Student[XPHEnum::Value]){
			$type_class=new StudentFeedbackType;
		}
		$web_role=new WebRole;
		$feedback_status=new FeedBackStatus;
		foreach ($data['ret'] as $k => $v) {
			$data['ret'][$k]['type_char']=$type_class->show($v['type']).$feedback_type_class->show($feedback_type);
			if($v['belong_char']!='--'){
				$data['ret'][$k]['status_char']=$web_role->show($v['role_belong']).'（已转发）'.$feedback_status->show($v['status']);
				
			}
			else{
				$data['ret'][$k]['status_char']=$web_role->show($v['role_belong']).$feedback_status->show($v['status']);
			}
			$data['ret'][$k]['created_char']=date('Y-m-d H:i:s',$v['created']);
			$data['ret'][$k]['img']=$this->feedback_service->getFeedBackImg($feedback_type,$v['id']);
			$data['ret'][$k]['manage_show']=true;
			//图标显示模式
			if($role==WebRole::Teacher[XPHEnum::Value]){
				$data['ret'][$k]['can_end']=false;
				$data['ret'][$k]['can_cancel_end']=false;
				$data['ret'][$k]['can_submit_to_manage']=false;
				$data['ret'][$k]['can_assign_deal_admin']=false;
				$data['ret'][$k]['can_show']=false;
				$data['ret'][$k]['can_write']=true;
			}
			else if($role==WebRole::Manage[XPHEnum::Value]){
				$data['ret'][$k]['can_end']=$this->feedback_service->checkEnd($feedback_type,$v['id']);
				$data['ret'][$k]['can_cancel_end']=$this->feedback_service->checkCancelEnd($feedback_type,$v['id']);
				$data['ret'][$k]['can_submit_to_manage']=false;
				$data['ret'][$k]['can_assign_deal_admin']=$this->feedback_service->checkAssignDealAdmin($feedback_type,$v['id']);
				$data['ret'][$k]['can_show']=false;
				$data['ret'][$k]['can_write']=true;
				if($v['role_belong']!=WebRole::Manage[XPHEnum::Value]){
					$data['ret'][$k]['manage_show']=false;
				}
			}
			else if($role==WebRole::UnitSupervisorGroupLeader[XPHEnum::Value]){
				$data['ret'][$k]['can_end']=$this->feedback_service->checkEnd($feedback_type,$v['id']);
				$data['ret'][$k]['can_cancel_end']=$this->feedback_service->checkCancelEnd($feedback_type,$v['id']);
				$data['ret'][$k]['can_submit_to_manage']=$this->feedback_service->checkSubmitToManage($feedback_type,$v['id']);
				$data['ret'][$k]['can_assign_deal_admin']=$this->feedback_service->checkAssignDealAdmin($feedback_type,$v['id']);
				$data['ret'][$k]['can_show']=false;
				$data['ret'][$k]['can_write']=true;
				if($v['role_belong']!=WebRole::UnitSupervisorGroupLeader[XPHEnum::Value]){
					$data['ret'][$k]['manage_show']=false;
					$data['ret'][$k]['can_write']=false;
				}
			}
			else if($role==WebRole::SupervisorDirector[XPHEnum::Value]){
				$data['ret'][$k]['can_end']=false;
				$data['ret'][$k]['can_cancel_end']=false;
				$data['ret'][$k]['can_submit_to_manage']=false;
				$data['ret'][$k]['can_assign_deal_admin']=false;
				$data['ret'][$k]['can_show']=$this->feedback_service->checksetShow($feedback_type,$v['id']);
				$data['ret'][$k]['can_write']=false;
			}
			else if($role==WebRole::SchoolLeader[XPHEnum::Value]){
				$data['ret'][$k]['can_end']=false;
				$data['ret'][$k]['can_cancel_end']=false;
				$data['ret'][$k]['can_submit_to_manage']=false;
				$data['ret'][$k]['can_assign_deal_admin']=false;
				$data['ret'][$k]['can_show']=false;
				$data['ret'][$k]['can_write']=false;
			}
			else{
				$data['ret'][$k]['can_end']=false;
				$data['ret'][$k]['can_cancel_end']=false;
				$data['ret'][$k]['can_submit_to_manage']=false;
				$data['ret'][$k]['can_assign_deal_admin']=false;
				$data['ret'][$k]['can_show']=false;
				$data['ret'][$k]['can_write']=false;
			}

			// $data['ret'][$k]['can_end']=$this->feedback_service->checkEnd($feedback_type,$v['id']);
			// $data['ret'][$k]['can_cancel_end']=$this->feedback_service->checkCancelEnd($feedback_type,$v['id']);
			// $data['ret'][$k]['can_submit_to_manage']=$this->feedback_service->checkSubmitToManage($feedback_type,$v['id']);
			// $data['ret'][$k]['can_assign_deal_admin']=$this->feedback_service->checkAssignDealAdmin($feedback_type,$v['id']);
			// $data['ret'][$k]['can_show']=$this->feedback_service->checksetShow($feedback_type,$v['id']);
			
		}
		if (!$isExport) {
			echo json_encode($data);
		} else {
			return $data["ret"];
		}
	}


	function showRecord($feedback_type,$id){
		$record=$this->feedback_service->getFeedBackRecord($feedback_type,$id);

		//将普通记录数组转化为时间线数组
		foreach ($record as $k => $v) {
			$record[$k]['created_day']=strtotime(date('Y-m-d',$v['created']));
		}
		$new_record=array();
		foreach ($record as $k => $v) {
			$new_record[$v['created_day']][]=$v;
		}
		$data['record']=$new_record;
		// var_dump($data['record']);
		// exit();
		$data['title']="反馈操作记录";
		$this->load->view('admin/showRecord',$data);
	}

	
	function doEnd($feedback_type,$id){
		$result=$this->feedback_service->endFeedBack($feedback_type,$id);
		exit($result);
	}

	function cancelEnd($feedback_type,$id){
		$result=$this->feedback_service->endFeedBack($feedback_type,$id,false);
		exit($result);
	}
	
	function submitToManage($feedback_type,$id){
		$result=$this->feedback_service->submitToManage($feedback_type,$id);
		exit($result);
	}

	function writeResult($feedback_type,$id){
		if($_POST['result']==''){
			exit(retJson('请填写处理意见和结果',false));
		}
		$result=$this->feedback_service->writeResult($feedback_type,$id,$_POST['result']);
		exit($result);
	}

	function getAdminList($feedback_type,$id){
		$admin=$this->feedback_service->getShareAdmin($feedback_type,$id,getSess()['id']);
		exit(retJson('用户列表',true,$admin));
	}

	function assignDealAdmin($feedback_type,$id){
		$result=$this->feedback_service->assignDealAdmin($feedback_type,$id,$_POST['belong']);
		exit($result);
	}

	function setShow($feedback_type,$id){
		$result=$this->feedback_service->setShow($feedback_type,$id);
		exit($result);
	}
	function getConfig($type){
		$this -> load ->model("Feedback_reply_config_model");
		$result = $this -> Feedback_reply_config_model ->get_list(array("status"=>CommonWhether::Whether[XPHEnum::Value],'type'=>$type));
		exit(retJson("返回类型",true,$result));
	}



}